import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/kwargs.dart';
import 'package:flutter_admin/utils/odoo_http_util.dart';

class StockQuantOdooApi {
  static Future<ResponseBodyApi> page({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      // var fieldsInfo = {
      //   'product.template': [
      //     'product_id',
      //     'default_code',
      //     'name',
      //     'description',
      //     'uom_id',
      //   ], //需要详细信息的model实体和字段,将获取product_code和product_name
      // };
      Kwargs recordsKwargs = Kwargs(
        domain: domainList,
        limit: pageModel.size,
        offset: (pageModel.current - 1) * pageModel.size,
        // order: pageModel.orders[0].column! +
        //     " " +
        //     (pageModel.orders[0].asc! ? "asc" : "desc")
      );
      var records = await OdooHttpUtil.callKw(
        model: 'stock.quant',
        method: 'stock_inquiry',
        kwargs: recordsKwargs.toMap(),
        //fieldsInfo: fieldsInfo,
        //filter: data["filter"]
      );
      // print(records.toString());

      // var total = 10;
      var total = await OdooHttpUtil.callKw(
          model: 'stock.quant',
          method: 'quant_data_statistics',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> getDataByComment(
      String comment, int limit) async {
    ResponseBodyApi responseBodyApi;
    try {
      List domainList = [
        ["comment", "=", comment]
      ];
      Kwargs kwargs =
          Kwargs(domain: domainList, limit: limit, order: "quantity desc");
      var data = await OdooHttpUtil.callKw(
          model: 'stock.quant',
          method: "search_plant_stock_data",
          kwargs: kwargs.toMap());
      responseBodyApi = ResponseBodyApi(success: true, data: data);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> getMaterialManagement() async {
    ResponseBodyApi responseBodyApi;
    try {
      var result = await OdooHttpUtil.callKw(
        model: 'stock.quant',
        method: 'Material_Management',
      );
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> getLocaitonManage() async {
    ResponseBodyApi responseBodyApi;
    try {
      var result = await OdooHttpUtil.callKw(
        model: 'locaiton_manage.locaiton_manage',
        method: 'stock_Keeper_data',
      );
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> removeByIds(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'stock.quant', method: "unlink", args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> saveOrUpdate(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      if (data["id"] == null) {
        await OdooHttpUtil.callKw(
            model: 'stock.quant', method: 'create', args: [data]);
      } else {
        await OdooHttpUtil.callKw(model: 'stock.quant', method: 'write', args: [
          [data["id"]],
          data
        ]);
      }
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> productOdoData(domainList) async {
    ResponseBodyApi responseBodyApi;
    try {
      Kwargs kwargs = Kwargs(domain: domainList);
      var result = await OdooHttpUtil.callKw(
          model: 'stock.quant',
          method: "product_odo_data",
          kwargs: kwargs.toMap());
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> shipTheProducts(domainList) async {
    ResponseBodyApi responseBodyApi;
    try {
      Kwargs kwargs = Kwargs(domain: domainList);
      var result = await OdooHttpUtil.callKw(
          model: 'stock.quant',
          method: "ship_the_products",
          kwargs: kwargs.toMap());
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> teamStockDataUpdate(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      var result = await OdooHttpUtil.callKw(
          model: 'stock.quant', method: "team_stock_data_update", args: [data]);
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> moveOutcomingSearchPage({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      // var fieldsInfo = {
      //   'product.template': [
      //     'product_id',
      //     'default_code',
      //     'name',
      //     'description',
      //     'uom_id',
      //   ], //需要详细信息的model实体和字段,将获取product_code和product_name
      // };
      Kwargs recordsKwargs = Kwargs(
        domain: domainList,
        limit: pageModel.size,
        offset: (pageModel.current - 1) * pageModel.size,
        // order: pageModel.orders[0].column! +
        //     " " +
        //     (pageModel.orders[0].asc! ? "asc" : "desc")
      );
      var records = await OdooHttpUtil.callKw(
        model: 'stock.move.line',
        method: 'move_outcoming_search',
        kwargs: recordsKwargs.toMap(),
        //fieldsInfo: fieldsInfo,
        //filter: data["filter"]
      );
      var total = await OdooHttpUtil.callKw(
          model: 'stock.move.line',
          method: 'move_outcoming_search_count',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> storageLocationRead(
      {required locationCode,
      required productCode,
      required productorderno}) async {
    ResponseBodyApi responseBodyApi;
    try {
      var records = await OdooHttpUtil.callKw(
          model: 'stock.quant',
          method: 'storage_location_read',
          kwargs: {
            "domain": [
              ['location_id.location_code', '=', locationCode],
              ['product_id.product_tmpl_id.default_code', '=', productCode],
              ['productorderno', '=', productorderno],
            ]
          });
      responseBodyApi = ResponseBodyApi(success: true, data: records);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> HWPage({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      // var fieldsInfo = {
      //   'product.template': [
      //     'product_id',
      //     'default_code',
      //     'name',
      //     'description',
      //     'uom_id',
      //   ], //需要详细信息的model实体和字段,将获取product_code和product_name
      // };
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
        model: 'stock.huawei_check',
        method: 'search_read',
        kwargs: recordsKwargs.toMap(),
      );
      var total = await OdooHttpUtil.callKw(
          model: 'stock.huawei_check',
          method: 'search_count',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> HWDetailPage({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      // var fieldsInfo = {
      //   'product.template': [
      //     'product_id',
      //     'default_code',
      //     'name',
      //     'description',
      //     'uom_id',
      //   ], //需要详细信息的model实体和字段,将获取product_code和product_name
      // };
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
        model: 'stock.quant',
        method: 'search_read',
        kwargs: recordsKwargs.toMap(),
      );
      var total = await OdooHttpUtil.callKw(
          model: 'stock.quant',
          method: 'search_count',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> HWSave(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      var result = await OdooHttpUtil.callKw(
          model: 'stock.huawei_check', method: 'to_create', args: [data]);
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> HWStorage(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      var result = await OdooHttpUtil.callKw(
          model: 'stock.huawei_check', method: 'huawei_storage', args: [data]);
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> getCountNum(checkId) async {
    ResponseBodyApi responseBodyApi;
    try {
      var completeNum = await OdooHttpUtil.callKw(
          model: 'stock.quant',
          method: 'search_count',
          kwargs: {
            'domain': [
              ['huawei_check_id', '=', checkId],
              ['is_packaging', '=', '1']
            ]
          });
      var finalNum = await OdooHttpUtil.callKw(
          model: 'stock.quant',
          method: 'search_count',
          kwargs: {
            'domain': [
              ['huawei_check_id', '=', checkId],
            ]
          });
      responseBodyApi =
          ResponseBodyApi(success: true, data: '$completeNum/$finalNum');
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> HWRequest(method, val) async {
    ResponseBodyApi responseBodyApi;
    try {
      var result = await OdooHttpUtil.callKw(
          model: 'stock.quant', method: method, kwargs: {'domain': val});
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> HWprint(method, val) async {
    ResponseBodyApi responseBodyApi;
    try {
      var result = await OdooHttpUtil.callKw(
          model: 'stock.huawei_check', method: method, kwargs: {'domain': val});
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> getScreen({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      var result = await OdooHttpUtil.callKw(
          model: 'stock.quant', method: 'Lcation_hdpi', args: [data]);
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  // static Future<ResponseBodyApi> HWSearchPage({data}) async {
  //   ResponseBodyApi responseBodyApi;
  //   try {
  //     PageModel pageModel = data['page'];
  //     List domainList = data['domainList'];
  //     Kwargs recordsKwargs = Kwargs(
  //         domain: domainList,
  //         limit: pageModel.size,
  //         offset: (pageModel.current - 1) * pageModel.size,
  //         order: pageModel.orders[0].column! +
  //             " " +
  //             (pageModel.orders[0].asc! ? "asc" : "desc")
  //     );
  //     var records = await OdooHttpUtil.callKw(
  //       model: 'stock.quant',
  //       method: 'huawei_search_read',
  //       kwargs: recordsKwargs.toMap(),
  //     );
  //     var total = await OdooHttpUtil.callKw(
  //         model: 'stock.quant',
  //         method: 'huawei_search_read_count',
  //         kwargs: {"domain": domainList});
  //     List<Map<String, dynamic>> list = [];
  //     records.forEach((element) {
  //       Map<String, dynamic> map = element;
  //       list.add(map);
  //     });
  //     pageModel.records = list;
  //     pageModel.total = total;
  //     pageModel.pages = total ~/ pageModel.size + 1;
  //     responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
  //   } catch (e) {
  //     responseBodyApi =
  //         ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
  //   }
  //   return responseBodyApi;
  // }

  // static Future<ResponseBodyApi> HWSearchDetailPage({data}) async {
  //   ResponseBodyApi responseBodyApi;
  //   try {
  //     PageModel pageModel = data['page'];
  //     List domainList = data['domainList'];
  //     Kwargs recordsKwargs = Kwargs(
  //         domain: domainList,
  //         limit: pageModel.size,
  //         offset: (pageModel.current - 1) * pageModel.size,
  //         order: pageModel.orders[0].column! +
  //             " " +
  //             (pageModel.orders[0].asc! ? "asc" : "desc")
  //     );
  //     var records = await OdooHttpUtil.callKw(
  //       model: 'stock.quant',
  //       method: 'huawei_search_info_read',
  //       kwargs: recordsKwargs.toMap(),
  //     );
  //     var total = await OdooHttpUtil.callKw(
  //         model: 'stock.quant',
  //         method: 'huawei_search_info_read_count',
  //         kwargs: {"domain": domainList});
  //     List<Map<String, dynamic>> list = [];
  //     records.forEach((element) {
  //       Map<String, dynamic> map = element;
  //       list.add(map);
  //     });
  //     pageModel.records = list;
  //     pageModel.total = total;
  //     pageModel.pages = total ~/ pageModel.size + 1;
  //     responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
  //   } catch (e) {
  //     responseBodyApi =
  //         ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
  //   }
  //   return responseBodyApi;
  // }
}
